package engine.newUi
{
	import newx.StageInfo;
	import flash.events.KeyboardEvent;
	import flash.events.Event;
	import newx.com.config.StaticIni;
	import flash.events.MouseEvent;
	import newx.com.control.window.NxWindowEvent;
	import flash.ui.Keyboard;
	import engine.ui.chat.ChatLogic;
	import engine.ui.chat.ChatEvent;
	import newx.com.control.scroll.text.ChatText;
	import flash.text.TextField;
	import engine.ui.FaceIconViewUI;
	import flash.display.SimpleButton;
	import flash.text.TextFieldType;
	import engine.data.ModelLocator;
	import engine.net.mgr.SystemSender;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.flag.GameDefine;
	import engine.logic.ChatFilterLogic;
	/**
	 *GMCC聊天界面 
	 * @author liuel
	 * 
	 */
	public class GmccLogic
	{
		private static var _self						:GmccLogic;
		private var _model						:ModelLocator;
		private  var ui                    	:WhisperUI;
		public static var enterobj					:Object;
		private var _viewBox               	:FaceIconViewUI;
		private var _chatLogic:ChatLogic;
		
		/**
		 *是否再GM私聊期间 
		 */		
		private var _isWhisper:Boolean;
		
		//私聊者的信息
		private var dataArr                	:Array;
		
		public static function getInstance():GmccLogic
		{
			if(_self ==null)
			{
				_self=new GmccLogic();
				
			}
			return _self;
		}
		public function GmccLogic()
		{
			ui = new WhisperUI(this);
		}
		public function Init():void
		{
			
			_chatLogic =ChatLogic.getInstance();
			_model =ModelLocator.getInstance();
			_chatLogic.addEventListener(ChatEvent.CHAT_GETGMMSG, getMsg);
			initUI();
		}
		public function get UI():FaceIconViewUI
		{
			if(_viewBox ==null)
			{
				_viewBox = new FaceIconViewUI();
				
			}
			return _viewBox;
		}
		public function get gmccUi():WhisperUI
		{
			return ui as WhisperUI;
		}
		private function initUI():void
		{
			ui.chatText = new ChatText();
			ui.inputTxt1 =new TextField();
			_viewBox		     = new FaceIconViewUI();
			_viewBox.showBtn     = ui.picBtn as SimpleButton;
			_viewBox.callBackFun = viewBoxFunction;
			
			//输出文本
			
			ui.chatText.x          = ui.outputArea.x+8;
			ui.chatText.y          = ui.outputArea.y+8;
			ui.chatText.scrollBarPos =ui.outputArea.width-25;
			ui.chatText.setSizeWH(ui.outputArea.width-25,ui.outputArea.height-10);
			//输入文本
			ui.inputTxt1.x        = ui.inputArea.x+8;
			ui.inputTxt1.y        = ui.inputArea.y+2;
			ui.inputTxt1.maxChars = 60;
			ui.inputTxt1.width    = ui.inputArea.width-10
			ui.inputTxt1.height   = ui.inputArea.height-2;
			ui.inputTxt1.type     = TextFieldType.INPUT;
			ui.inputTxt1.wordWrap = true;
			ui.inputTxt1.textColor= 0xffffff;
			ui.inputTxt1.text     = "";
			if(dataArr!=null)
			{
				ui.picClip.source="res/photos/head_"+ dataArr[10] + ".png";
			}
			
			ui.windowBG  .addChild(ui.chatText);
			ui.windowBG  .addChild(ui.inputTxt1);
			
			ui.inputTxt1.visible  = true;
			ui.label1.visible = ui.mc1.visible=ui.picBtn .visible=ui.nextBtn.visible=ui.label4.visible=ui.mc4.visible=false;
			ui.addBtn.visible=ui.addblackBtn.visible =ui.nameLabel.visible =ui.levelLabel.visible =ui.picbg_1.visible =ui.picClip.visible=false;
		}
		public function BeforeShow():void
		{
			ui.minBtn.visible=false;
			ui.windowBG.titleIcon="ServiceUIPic"
			addListener()
		}
		public function AfterHide():void
		{
			removeListener();
		}
		private function removeListener():void
		{
			ui.windowBG .removeEventListener(NxWindowEvent.WINDOW_CLOSE,                onClickClose);
			ui.sendBtn  .removeEventListener(MouseEvent.CLICK,                          onClicksend);
			
			ui.inputTxt1.removeEventListener(Event.CHANGE,								onChange);
			ui.picBtn   .removeEventListener(MouseEvent.CLICK,                        onPicBox);
			ui.inputTxt1.removeEventListener(MouseEvent.CLICK,								onClickInput);
			StageInfo.stage.removeEventListener(KeyboardEvent.KEY_DOWN,                 onEnter);
		}
		private function addListener():void
		{  
			ui.windowBG .addEventListener(NxWindowEvent.WINDOW_CLOSE,                   onClickClose);
			ui.sendBtn  .addEventListener(MouseEvent.CLICK,                             onClicksend);
			ui.picBtn   .addEventListener(MouseEvent.CLICK,                        onPicBox);
			ui.inputTxt1.addEventListener(Event.CHANGE,								onChange);
			ui.inputTxt1.addEventListener(MouseEvent.CLICK,								onClickInput);
			
			StageInfo.stage.addEventListener(KeyboardEvent.KEY_DOWN,                    onEnter);
			
		}
		/**
		 * viewBox(表情)回调函数 
		 * @param argc
		 * 
		 */		
		public  function viewBoxFunction(...argc):void
		{
			
			if(ui.inputTxt1.text.length > ui.inputTxt1.maxChars - argc[0].length)
			{
				return;
			}
			
			ui.inputTxt1.replaceText(ui.inputTxt1.caretIndex ,ui.inputTxt1.caretIndex ,argc[0]);
			_viewBox.hide();
		}
		/**
		 * 按下表情按钮 
		 * @param e
		 * 
		 */		
		private function onPicBox(e:MouseEvent):void
		{
			
			if(this._viewBox.created)
			{
				_viewBox.removeStageEvent();
				_viewBox.hide();
			}
			else
			{
				_viewBox.show(ui);
				_viewBox.x=ui.picBtn.x-50;
				_viewBox.y=ui.picBtn.y;
			}
		}
		private function htmlCode(content : String) : String
		{
			var msg : String = content;
			var myPattern:RegExp = new RegExp("<","g");  
			var myPattern1:RegExp = new RegExp(">","g");  
			
			msg = msg.replace(myPattern,"&lt;");
			msg = msg.replace(myPattern1,"&gt;");
			return msg;
		}
		/**
		 *发送聊天信息 
		 * 
		 */		 
		private function sendMsg(isHtml: Boolean):void
		{
			var message:String = ui.inputTxt1.text;
			if(message=="") return;
			
			if(isHtml)
			{
				message = htmlCode(message);
			}
			
			//发送后清空输入框
			if(message.indexOf("/") != 0)
			{
				ui.inputTxt1.text = "";
			}
			else
			{
				ui.inputTxt1.text	=	message.substr(0, message.indexOf(" ") + 1);
			}
			_model.dispatchEvent(new CustomMsgEvent(String(CustomToClientFlag.SERVER_CUSTOMMSG_CHAT),[GameDefine.CHATTYPE_TOGM, ChatFilterLogic.getInstance().filter(message)]));
			SystemSender.getInstance().SendGMCCTOServer(message);
			
		}
		/**
		 *获取GM会话是否结束 
		 * @return 
		 * 
		 */		
		public function get gmHelpStage():Boolean
		{
			return _isWhisper;
		}
		public function set gmHelpState(val:Boolean):void
		{
			_isWhisper =val;
		}
		private function getMsg(e:ChatEvent):void
		{
			
			var chatList : Array = e._data as Array;//getChatContentStr();
			if (chatList != null ) 
			{
				var str:String = chatList[1]
				if(str.indexOf("#EXTE#gmVote#")!=-1)  //聊天会话终止，要求客户端让玩家对GM投票
				{
					//退出
					str =str.replace("#EXTE#gmVote#","");
					GmccOpinionLogic.getInstance().title =str;
					GmccOpinionUI.getInstance().showOrHide();
					_isWhisper =false;
					clearGmccWhisperInfo();
					return;
			
				}
				else if(str.indexOf("#EXTE#regardRes")!=-1)  //获取GMCC的服务种类
				{
					str=str.replace("</font><br/>","");
					var infoArr:Array =[];
					infoArr = str.split("#");
					var typeArr:Array =infoArr[7].toString().split(";"); //问题类型【一般问题，充值问题，BUG问题，投诉】
					var appraiseType:Array = infoArr[8].toString().split(";");
					GmccFunctionLogic.getInstance().questionName =typeArr;
					GmccOpinionLogic.getInstance().type =appraiseType
					return;
				}
				else if(str.indexOf("#EXTE#gmRegard")!=-1)  //初始化信息 不需要显示
				{
					return;
				}
				if(gmccUi.created==false)
				{
					gmccUi.showOrHide();
				}
				ui.chatText.insterTalkStr1(str);
			}
		}
		/**
		 * 快捷键
		 * 回车显示输入框，有文字发送，没文字关闭输入 
		 * @param e
		 * 
		 */
		private function onEnter(e:KeyboardEvent):void
		{
			if(enterobj ==null) return;
			if(e.keyCode	==	Keyboard.ENTER)
			{
				if(enterobj ==null ||enterobj !=ui.inputTxt1) return;
				if(ui.inputTxt1.text!="")
				{
					sendMsg(true);
				}
				StageInfo.stage.focus	=	ui.inputTxt1;
			}
			
		}
		/**
		 * 点击关闭 
		 * @param e
		 * 
		 */	    
		private function onClickClose(e:NxWindowEvent):void
		{

			ui.hide();
		}
		public function clearGmccWhisperInfo():void
		{
			ui.inputTxt1.text="";
			ui.chatText.clearTalkText();
		}
		/**
		 * 点击发送 
		 * @param e
		 * 
		 */			
		private function onClicksend(e:MouseEvent):void
		{
			if(ui.inputTxt1.text!="")
			{
				sendMsg(true);
			}
			enterobj = ui.inputTxt1;
		}
		private function onClickInput(e:MouseEvent):void
		{
			if(TopOtherPlayerLogic.getInstance().ui.created)
			{
				TopOtherPlayerLogic.getInstance().ui.hide();
			}
			enterobj=ui.inputTxt1;
			StageInfo.stage.focus	=	ui.inputTxt1;
			
		}
		private function onChange(e:Event):void
		{
			var val:int =ui.inputTxt1.maxChars-ui.inputTxt1.text.length;
			ui.textLabel.htmlText =StaticIni.getSwfLangStrVar("UIString_1000109",[val]);
		}
	}
}